package org.example.service;

import com.alibaba.fastjson.JSON;
import org.example.mapper.RegionMapper;
import org.example.pojo.Region;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RegionService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RegionMapper regionMapper;

    public List<Region> selectType() {
        if (stringRedisTemplate.hasKey("type")){
            String type = stringRedisTemplate.opsForValue().get("type");
            return JSON.parseArray(type,Region.class);
        }
        List<Region> list = regionMapper.selectType();
        stringRedisTemplate.opsForValue().set("type",JSON.toJSONString(list));
        return list;
    }
}
